package com.wkbb.sports.controller;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.sports.dto.SportsHomeInfoDto;
import com.wkbb.sports.service.WorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(description = "首页数据接口")
@RequestMapping("/sports/{apiVersion}/home")
@Slf4j
public class HomeController extends BaseController {

    @Autowired
    private WorkService workService;

    @ApiOperation(value = "小程序首页")
    @RequestMapping(value = "/miniFamilyHome", method = RequestMethod.GET)
    public ResultDTO miniFamilyHome(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                      @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
                                      @ApiParam(value = "学生ID", required = true) @RequestParam(name = "studentId", required = true) Long studentId,
                                      @ApiParam(value = "页码", required = false) @RequestParam(name = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                      @ApiParam(value = "每页条数", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize)
    {
        UserDto userDto = this.getRouteUserByUnion();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }

        if(null==orgId||null==studentId)
        {
            return new ResultDTO<>(ErrorCode.PARAM_MISSING);
        }

        log.info("miniHome orgId:{} userId:{}", orgId, userDto.getId());

        try {

            Map<String,Object> params = new HashMap<>();
            params.put("orgId",orgId);
            params.put("studentId",studentId);

            List<SportsHomeInfoDto> studentWorkList = workService.getStudentWorkList(params,pageNo,pageSize);
            return new ResultDTO(studentWorkList);

        } catch (Exception e) {
            log.error("***********miniHome 异常******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }
}
